' Just to register all created objects, and then free memore after them

' List of all classes that support InstanceMonitor

Class InstanceMonitor
	Private colInstances
	
	Private Sub Class_Initialize()
		Set colInstances = New ArrayList
	End Sub
	
	Public Sub AddInstance(ByVal objInstance)
		colInstances.Add(objInstance)
	End Sub
	
	' It should be the method that return dispose method of appropriate class
	Public Sub Dispose()
		For i=1 To colInstances.Count
			On Error Resume Next
				If TypeName(colInstances(i)) = "IE" Then
					colInstances(i).Close()
				ElseIf TypeName(colInstances(i)) = "Database" Then
					colInstances(i).Disconnect()
				ElseIf TypeName(colInstances(i)) = "IEManager" Then
					colInstances(i).CloseAll()
				ElseIf TypeName(colInstances(i)) = "Launcher" Then
					colInstances(i).Close()
				End If
				
				Set colInstances(i) = Nothing
			On Error GoTo 0
		Next
		
		Set colInstances = Nothing
	End Sub
End Class

Class Extension
	Private strExtensionName
	Private objPrivateInstanceMonitor
	
	Public Property Set InstanceMonitor(ByVal objValue)
		Set objPrivateInstanceMonitor = objValue
	End Property
	
	' Ext("Web::IEManager").GetInstance() '.Initialize() -> Set ... = New IEManager
	Public Function GetInstance
		On Error Resume Next
			ExecuteGlobal "Set objInstance = New " & strExtensionName
		
			If Err Then
				Err.Clear
				
				ExecuteGlobal "Set objInstance = CreateObject(""" & strExtensionName & """)"
			End If
			
			If Err Then Exit Function
		On Error GoTo 0
		
		objPrivateInstanceMonitor.AddInstance(objInstance)
		
		Set GetInstance = objInstance
	End Function

	Public Default Function ExtensionItem(ByVal strParameterExtensionName)
		strExtensionName = strParameterExtensionName
		
		Set ExtensionItem = Me
	End Function
End Class

Class ExtensionLoader
	Public Sub Load()
		strExtensionsFolderPath = GLOBAL_APPLICATION_PATH & "lib\Extensions"

		Set objSys = New Sys
		Set objXmlDocument = CreateObject("MSXML2.DOMDocument.6.0")
		
		objXmlDocument.Async = False

		If Not objXmlDocument.Load(strExtensionsFolderPath & "\extensions.config.xml") Then
			'Err.Raise 5000, "ExtensionLoader->Load", "Error occured during loading extension Xml file"
		End If

		Set colXmlExtension = objXmlDocument.GetElementsByTagName("Extension")
		
		For i=0 To colXmlExtension.Length-1
			If colXmlExtension(i).GetAttribute("Enable") = "True" Then
				strExtensionName = colXmlExtension(i).GetAttribute("Name")

				Set colXmlLibrary = colXmlExtension(i).ChildNodes

				For j=0 To colXmlLibrary.Length-1
					strName = colXmlLibrary(j).GetAttribute("Name")
					strProgId = colXmlLibrary(j).GetAttribute("ProgId")
					strFileName = colXmlLibrary(j).GetAttribute("FileName")
					strType = colXmlLibrary(j).GetAttribute("Type")

					strPathToLibrary = objSys.GetPathToFileByFileName(strExtensionsFolderPath & "\" & strExtensionName & "\", strFileName)
		
					If strPathToLibrary = "" Then
						Err.Raise 5000, "ExtensionLoader->Load", "Error Description 2"
					End If
				Next

				' regsvr32.exe, regasm.exe
				
				'Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
				'Set objFile = objFso.OpenTextFile(strPathToTestScript)
		
				'strTestScript = objFile.ReadAll()

				'objFile.Close()
			End If
		Next
	End Sub

	Public Default Function Extensions(ByVal strExtensionName)
	End Function
	
	Private Sub RegisterLibrary(ByVal strLibraryName, ByVal strLibraryType, ByVal strProgId)
		If IsLibraryRegistered(strProgId) Then Exit Sub
		
		If strLibraryType = "COM" Then
			'// bind a variabe to WScript.Shell
'Set WshShell = CreateObject("WScript.Shell")

'// define the path to the regasm.exe file
'RegAsmPath = "c:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe"

'// register the dll
'WshShell.run "cmd /c " & RegAsmPath & " c:\temp\cbsecurity.dll /codebase /nologo /s", 0, True ' True, 1


			Exit Sub
		End If

		If strLibraryType = ".NET COM" Then


			Exit Sub
		End If

		' Error raise, that such library type doesn't supported
	End Sub

	Private Function CreateInstance()
		
	End Function
	
	Private Function IsLibraryRegistered(ByVal strProgId)
		IsLibraryRegistered = True

		On Error Resume Next
			Set objObjectPId = WScript.CreateObject(strProgId)

			If Err Then IsLibraryRegistered = False
		On Error Goto 0
	End Function
End Class